package com.zdkj.subway.check.service.impl.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zdkj.subway.check.entity.*;
import com.zdkj.subway.check.mapper.*;
import com.zdkj.subway.check.util.ObejctFieldsValueNotNull;
import com.zdkj.subway.check.util.UserInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * tvm层级差异待协查
 */
@Service
public class TvmChaYiDXCService {

    @Resource
    private SwAuTvmMoneyAssistMapper swAuTvmMoneyAssistMapper;
    @Resource
    private SwAuTvmSingleAssistMapper swAuTvmSingleAssistMapper;
    @Resource
    private SwAuTvmDailyBillAssistMapper swAuTvmDailyBillAssistMapper;
    @Resource
    private SwAuTvmDailyBillAssistDetailMapper swAuTvmDailyBillAssistDetailMapper;
    @Resource
    private SwStTvmDailyBillMapper swStTvmDailyBillMapper;
    @Resource
    SwStTvmDailyBillCashPayMapper swStTvmDailyBillCashPayMapper;
    @Resource
    SwStTvmDailyBillElectronicPayMapper swStTvmDailyBillElectronicPayMapper;

    public List<Map<String, Object>> getTvmTicketCYXCList(Map<String,String> param) {
        List<Map<String, Object>> res=swAuTvmMoneyAssistMapper.getTvmTicketCYXCList(param);
        return res;
    }

    /**
     * TVM单台设备票款差异协查
     * @param swAuTvmSingleAssist
     * @return
     */
    public List<Map<String, Object>> getSwAuTvmSingleAssistList(SwAuTvmSingleAssist swAuTvmSingleAssist) {
        List<Map<String, Object>> res=swAuTvmSingleAssistMapper.getSwAuTvmSingleAssistList(swAuTvmSingleAssist);
        return res;
    }

    /**
     * 结账单据协查
     * @param swAuTvmMoneyAssist
     * @return
     */
    public List<Map<String, Object>> getSwAuTvmDailyBillAssistList(SwAuTvmDailyBillAssist swAuTvmMoneyAssist) {
        HashMap req = JSONObject.parseObject(JSONObject.toJSONString(swAuTvmMoneyAssist), HashMap.class);
        List<Map<String, Object>> res=swAuTvmDailyBillAssistMapper.getSwAuTvmDailyBillAssistList(req);
        return res;
    }

    /**
     * 获取结账单详情信息
     * @param swAuTvmMoneyAssist
     * @return
     */
    public List<Map<String, Object>> getSwAuTvmDailyBillAssistDetail(SwAuTvmDailyBillAssistDetail swAuTvmMoneyAssist) {
        List<Map<String, Object>> res=swAuTvmDailyBillAssistDetailMapper.getSwAuTvmDailyBillAssistDetail(swAuTvmMoneyAssist);
        return res;
    }

    /**
     * TVM日结账单据录入
     * @param swStTvmDailyBill
     * @return
     */
    public List<Map<String, Object>> getSwStTvmDailyBillList(SwStTvmDailyBill swStTvmDailyBill) {
        List<Map<String, Object>> res=swStTvmDailyBillMapper.getSwStTvmDailyBillList(swStTvmDailyBill);
        return res;
    }

    /**
     * 添加TVM日结账单据录入-现金，电子表
     * @param cashs
     * @param electrons
     * @param swStTvmDailyBill
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer addSwStTvmDailyBills(String cashs, String electrons, SwStTvmDailyBill swStTvmDailyBill) throws InstantiationException, IllegalAccessException ,Exception{
        JSONArray cashsArr = JSONArray.parseArray(cashs);
        JSONArray electronsArr = JSONArray.parseArray(electrons);
        swStTvmDailyBill.setDeptId(UserInfo.getDeptId());
        swStTvmDailyBill.setCreatorId(UserInfo.getId());
        swStTvmDailyBill.setOperatorName(UserInfo.getUserName());
        swStTvmDailyBill.setCreatorName(UserInfo.getUserName());
        swStTvmDailyBill.setOperatorJobNo(UserInfo.getUserCode());
        //为空说明需要添加
        if(swStTvmDailyBill.getId()==null){
            swStTvmDailyBill.setDataSource("1");
            swStTvmDailyBill=(SwStTvmDailyBill)ObejctFieldsValueNotNull.setValueNoNull(SwStTvmDailyBill.class,swStTvmDailyBill);
            int insert = swStTvmDailyBillMapper.insert(swStTvmDailyBill);
        }else{
//            Map<String,Object> param=new HashMap<>();
//            param.put("relation_id",swStTvmDailyBill.getId());
            swStTvmDailyBillMapper.updateById(swStTvmDailyBill);
//            swStTvmDailyBillCashPayMapper.deleteByMap(param);
//            swStTvmDailyBillElectronicPayMapper.deleteByMap(param);
        }

        //充值
        BigDecimal deposit=new BigDecimal(0);
        //售卖
        BigDecimal sals=new BigDecimal(0);
        //退款
        BigDecimal ret=new BigDecimal(0);
        for(int i=0;i<cashsArr.size();i++){
            JSONObject jsonObject = cashsArr.getJSONObject(i);
            SwStTvmDailyBillCashPay swStTvmDailyBillCashPay = JSONObject.parseObject(jsonObject.toJSONString(), SwStTvmDailyBillCashPay.class);
            swStTvmDailyBillCashPay.setRelationId(swStTvmDailyBill.getId());
            //////统计金额
            deposit=deposit.add(swStTvmDailyBillCashPay.getRechargeMoney());
            sals=sals.add(swStTvmDailyBillCashPay.getSellTicketTotalMoney());
            ret=ret.add(swStTvmDailyBillCashPay.getFaultRefundAmount());
            if(swStTvmDailyBillCashPay.getId()==null){
                swStTvmDailyBillCashPay.setDelFlag("0");
                swStTvmDailyBillCashPay.setTvmCode(swStTvmDailyBill.getTvmCode());
                SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
                swStTvmDailyBillCashPay.setStartTime(dff.parse(swStTvmDailyBill.getStartTime()));
                swStTvmDailyBillCashPay.setEndTime(dff.parse(swStTvmDailyBill.getEndTime()));
                swStTvmDailyBillCashPay=(SwStTvmDailyBillCashPay)ObejctFieldsValueNotNull.setValueNoNull(SwStTvmDailyBillCashPay.class,swStTvmDailyBillCashPay);
                swStTvmDailyBillCashPayMapper.insert(swStTvmDailyBillCashPay);
            }else{
                swStTvmDailyBillCashPayMapper.updateById(swStTvmDailyBillCashPay);
            }
        }
        //充值+售卖-退款=现金总计
        swStTvmDailyBill.setDailyCashTotalAmount(deposit.add(sals).subtract(ret));

        //充值
        BigDecimal e_deposit=new BigDecimal(0);
        //售卖
        BigDecimal e_sals=new BigDecimal(0);
        //退款
        BigDecimal e_ret=new BigDecimal(0);
        for(int i=0;i<electronsArr.size();i++){
            JSONObject jsonObject = electronsArr.getJSONObject(i);
            SwStTvmDailyBillElectronicPay swStTvmDailyBillElectronicPay = JSONObject.parseObject(jsonObject.toJSONString(), SwStTvmDailyBillElectronicPay.class);
            swStTvmDailyBillElectronicPay.setRelationId(swStTvmDailyBill.getId());
            ///////统计金额
            e_deposit=e_deposit.add(swStTvmDailyBillElectronicPay.getRechargeMoney());
            e_sals=e_sals.add(swStTvmDailyBillElectronicPay.getSellTicketMoney());
            e_ret=e_ret.add(swStTvmDailyBillElectronicPay.getFaultRefundAmount());
            if(swStTvmDailyBillElectronicPay.getId()==null){
                swStTvmDailyBillElectronicPay.setDelFlag("0");
                swStTvmDailyBillElectronicPay.setTvmCode(swStTvmDailyBill.getTvmCode());
                SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
                swStTvmDailyBillElectronicPay.setStartTime(dff.parse(swStTvmDailyBill.getStartTime()));
                swStTvmDailyBillElectronicPay.setEndTime(dff.parse(swStTvmDailyBill.getEndTime()));
                swStTvmDailyBillElectronicPay=(SwStTvmDailyBillElectronicPay)ObejctFieldsValueNotNull.setValueNoNull(SwStTvmDailyBillElectronicPay.class,swStTvmDailyBillElectronicPay);
                swStTvmDailyBillElectronicPayMapper.insert(swStTvmDailyBillElectronicPay);
            }else {
                swStTvmDailyBillElectronicPayMapper.updateById(swStTvmDailyBillElectronicPay);
            }
        }
        //充值+售卖-退款=电子总计
        swStTvmDailyBill.setElectronicPayTotalAmount(e_deposit.add(e_sals).subtract(e_ret));

        //最后修改金额
        swStTvmDailyBillMapper.updateById(swStTvmDailyBill);
        return 1;
    }
}
